<?php
declare (strict_types=1);

namespace app\index\controller;

use nxcsoft\wechat\Pay;
use SingKa\Sms\SkSms;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Db;

class Index
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 75px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) 网站应用层</h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
    }

    /**
     * 单元测试方法
     */
    public function test()
    {
        $newpay = new Pay();

        $newpay->jsApiPay('744-1703252869', 1, 'odeqM6-J5ad3d2cNVLTr3ogMkc48	', $attach = '订单支付', $profit_sharing = false);
//        $res = $newpay->refunds('744-1703252869', 'TK744-1703252869', 50, 50, $reason = '');
        halt(123);
    }

    /**
     * @return mixed|string
     * 返回baseurl
     */
    private function getBaseUrl()
    {
        $configData = Cache::get('storage');
        $configData = json_decode($configData, true);
        switch ($configData['name']) {
            case 'qiniu':
                $baseUrl = $configData['config']['url'];
                break;
            case 'aliyun':
                $baseUrl = $configData['config']['url'];
                break;
            case 'qcloud':
                $baseUrl = $configData['config']['scheme'] . '://' . $configData['cdn'];
                break;
        }
        return $baseUrl;

    }

    /**
     * 短信发送示例
     *
     * @mobile  短信发送对象手机号码
     * @action  短信发送场景，会自动传入短信模板
     * @parme   短信内容数组
     * 是从数据库中读取的短信默认驱动
     *
     */
    public function sendSms()
    {

        $mobile = '15939918942';
        $action = 'register';//操作的方法名称
        $parme = ['code' => 200, 'name' => 1000];
        $SmsConfig = false;
        $SmsDefaultDriver = $this->getConfig();//驱动
        //$this->SmsConfig是从数据库中读取的短信配置
        $config = $SmsConfig ?: Config::get('sms.' . $SmsDefaultDriver);
        $sms = new SkSms($SmsDefaultDriver, $config);//传入短信驱动和配置信息
        //判断短信发送驱动，非阿里云和七牛云，需将内容数组主键序号化
        switch ($SmsDefaultDriver) {
            case 'aliyun':
                $result = $sms->$action($mobile, $parme);
                break;
            case 'qiniu':
                $result = $sms->$action([$mobile], $parme);
                break;
            case 'upyun':
                $result = $sms->$action($mobile, implode('|', $this->restoreArray($parme)));
                break;
            default:
                $result = $sms->$action($mobile, $this->restoreArray($parme));
                break;
        }
        if ($result['code'] == 200) {
            $data['code'] = 200;
            $data['msg'] = '短信发送成功';
        } else {
            $data['code'] = $result['code'];
            $data['msg'] = $result['msg'];
        }
        echo \GuzzleHttp\json_encode($data);
        die;
        return $data;
    }

    /**
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *短信配置
     */
    public function getConfig()
    {
        $smsengine = Cache::get('smsengine');
        if (!empty($smsengine)) {
            return $smsengine;
        }
        $smsInfo = Db::name('config_data')
            ->where('code', 'sendsms')
            ->field('value,options')
            ->find();
//        $data = preg_split('/[\r\n]+/s', $storageInfo['options']);
//        if ($data) {
//            $arr = [];
//            foreach ($data as $vt) {
//                $value = preg_split('/[:：]+/s', $vt);
//                $arr[$value[0]] = $value[1];
//            }
//        }
//        $name = $arr[$storageInfo['value']];
//
//        $configs = Db::name('config')
//            ->where('name', $name)
//            ->field('id,code')->find();
//        $configId = $configs['id'];
//        $configInfo = Db::name('config_data')
//            ->where('config_id', $configId)
//            ->field('value,code')
//            ->select()->toArray();
//        $config = array_column($configInfo, 'value', 'code');
//        $name = $configs['code'];
//        $configData = [
//            'name' => $name,
//            'config' => $config,
//        ];
        $smsengine = $smsInfo['value'];
        Cache::set('smsengine', $smsengine, 86400);
        return $smsengine;
    }

    /**
     * 数组主键序号化
     *
     * @arr  需要转换的数组
     */
    public function restoreArray($arr)
    {
        if (!is_array($arr)) {
            return $arr;
        }
        $c = 0;
        $new = [];
        foreach ($arr as $key => $value) {
            $new[$c] = $value;
            $c++;
        }
        return $new;
    }
}
